<?php

namespace Common\Model;

use Think\Model;

class UserPmsModel extends Model
{
    protected $_auto = array(
        array('send_display', 1),
        array('to_display', 1),
        array('send_read', 1),
        array('to_read', 1),
    );

    /**
     * 添加会话
     *
     * @param string $data
     */
    public function add_pms($data = '')
    {
        $send_uid = $data['send_uid'];
        $to_uid   = $data['to_uid'];
        $msg      = $data['msg'];

        $alias = $this->get_pms_alias($send_uid, $to_uid);

        // 创建私信会话 [会话未注册时]
        $pms = $this->get_pms_one($alias, 2);
        if (empty($pms)) {
            $pmsdata['alias']    = $alias;
            $pmsdata['send_uid'] = intval($send_uid);
            $pmsdata['to_uid']   = intval($to_uid);
            if ($this->create($pmsdata)) {
                $pmsid = $this->add();
                $pms   = $this->get_pms_one($pmsid);
            }
        }

        $chatdata['pid']      = $pms['id'];
        $chatdata['msg']      = $msg;
        $chatdata['send_uid'] = intval($send_uid);
        $chatdata['to_uid']   = intval($to_uid);
        // 添加私信会话内容
        $pms_chat = D('UserPmsChat');
        if ($pms_chat->create($chatdata)) {
            $pms_chat->add();
        }

        $read['id']           = $pms['id'];
        $read['send_display'] = 1;
        $read['to_display']   = 1;
        if ($pms['send_uid'] == $to_uid) {
            $read['send_read'] = 0;
        } else {
            $read['to_read'] = 0;
        }
        M('UserPms')->save($read);

        //        //设置发件人已读
        //        $read['id'] = $pms['id'];
        //        if ($pms['send_uid'] == $send_uid) {
        //            $read['send_read'] = 1;
        //        } else {
        //            $read['to_read'] = 1;
        //        }
        //        M('UserPms')->save($read);
    }

    /**
     * 获取消息
     *
     * @param string $map
     * @param bool   $page
     * @param string $num
     * @param string $myuid
     * @return mixed
     */
    public function get_list($map = '', $page = false, $num = '', $myuid = '')
    {
        $limit = $num ? $num : 10;
        $sort  = "id DESC";
        if ($page) {
            $count    = $this->where($map)->count();
            $Page     = pager($count, $limit);
            $limit    = $Page->firstRow . ',' . $Page->listRows;
            $page_str = $Page->fshow();
        }
        $result = $this->where($map)->order($sort)->limit($limit)->select();
        foreach ($result as $key => $v) {
            $v['url'] = $this->view_url($v['id']);
            if ($v['send_uid'] == $myuid) {
                $v['user'] = D('User')->get_user_one($v['to_uid'], true, 1);
            } else {
                $v['user'] = D('User')->get_user_one($v['send_uid'], true, 1);
            }
            $v['reply']       = D('UserPmsChat')->get_one($v['id']);
            $v['addtime_str'] = daterange($v['reply']['addtime']);
            $list[]           = $v;
        }
        $return['list']  = $list;
        $return['count'] = $page ? $count : count($list);
        $return['page']  = $page_str;
        //print_r($return);
        return $return;
    }

    /**
     * 设置消息已读未读
     *
     * @param string $id
     * @param string $myuid
     * @param string $read
     */
    public function set_pms_read($id = '', $myuid = '', $read = '')
    {
        $map['id'] = intval($id);
        $pms       = $this->get_pms_one($id);
        if ($pms['send_uid'] == $myuid) {
            $data['send_read'] = intval($read);
            if ($read === '') {
                $data['send_read'] = $pms['send_read'] ? 0 : 1;
            }
        } else {
            $data['to_read'] = intval($read);
            if ($read === '') {
                $data['to_read'] = $pms['to_read'] ? 0 : 1;
            }
        }
        $this->where($map)->save($data);
    }

    /**
     * 设置会话显示
     *
     * @param string $id
     */
    public function set_pms_display($id = '')
    {
        $data['id']           = intval($id);
        $data['send_display'] = 1;
        $data['to_display']   = 1;
        $this->save($data);
    }

    /**
     * 获取私信
     *
     * @param string $v
     * @param string $type 1-id 2-alias
     * @return mixed
     */
    public function get_pms_one($v = '', $type = '1')
    {
        if ($type == '1') {
            $map['id'] = intval($v);
        }
        if ($type == '2') {
            $map['alias'] = trim($v);
        }
        $pms = $this->where($map)->find();
        return $pms;
    }

    /**
     * 获取会话组别名
     *
     * @param string $send_uid
     * @param string $to_uid
     * @return string
     */
    public function get_pms_alias($send_uid = '', $to_uid = '')
    {
        $send_uid = intval($send_uid);
        $to_uid   = intval($to_uid);
        $alias    = "{$send_uid}|{$to_uid}";
        if ($send_uid > $to_uid) {
            $alias = "{$to_uid}|{$send_uid}";
        }
        return $alias;

    }

    /**
     * 获取详细页的URL
     *
     * @param $id
     * @return string
     */
    public function view_url($id)
    {
        return RU('user/pms_view', array('id' => $id));
    }
}